package admin

import (
	"bbs-go/model"
	"bbs-go/services"
	"github.com/kataras/iris/v12"
	"github.com/mlogclub/simple"
	"strconv"
)

type ArticleTagController struct {
	Ctx iris.Context
}

/**
 * showdoc
 * @catalog admin/文章标签
 * @title 获取标签信息
 * @method get
 * @url /api/article-tag/{id}
 * @param id 必选 int64 标签id
 */
func (this *ArticleTagController) GetBy(id int64) *simple.JsonResult {
	t := services.ArticleTagService.Get(id)
	if t == nil {
		return simple.JsonErrorMsg("Not found, id=" + strconv.FormatInt(id, 10))
	}
	return simple.JsonData(t)
}

/**
 * showdoc
 * @catalog admin/文章标签
 * @title 获取标签信息
 * @method any
 * @url /api/article-tag/{id}
 * @param id 必选 int64 标签id
 */
func (this *ArticleTagController) AnyList() *simple.JsonResult {
	list, paging := services.ArticleTagService.FindPageByParams(simple.NewQueryParams(this.Ctx).PageByReq().Desc("id"))
	return simple.JsonData(&simple.PageResult{Results: list, Page: paging})
}

/**
 * showdoc
 * @catalog admin/文章标签
 * @title 添加文章标签
 * @method post
 * @url /api/article-tag/create
 * @param articleId 必选 int64 文章id
 * @param tagId 必选 int64 标签id
 */
func (this *ArticleTagController) PostCreate() *simple.JsonResult {
	t := &model.ArticleTag{}
	this.Ctx.ReadForm(t)

	err := services.ArticleTagService.Create(t)
	if err != nil {
		return simple.JsonErrorMsg(err.Error())
	}
	return simple.JsonData(t)
}

/**
 * showdoc
 * @catalog admin/文章标签
 * @title 添加文章标签
 * @method post
 * @url /api/article-tag/update
 * @param id 必选 int64 ArticleTag_id
 * @param articleId 必选 int64 文章id
 * @param tagId 必选 int64 标签id
 */
func (this *ArticleTagController) PostUpdate() *simple.JsonResult {
	id, err := simple.FormValueInt64(this.Ctx, "id")
	if err != nil {
		return simple.JsonErrorMsg(err.Error())
	}
	t := services.ArticleTagService.Get(id)
	if t == nil {
		return simple.JsonErrorMsg("entity not found")
	}

	this.Ctx.ReadForm(t)

	err = services.ArticleTagService.Update(t)
	if err != nil {
		return simple.JsonErrorMsg(err.Error())
	}
	return simple.JsonData(t)
}
